580B - Kefa and Company - CodeForces Solution


binary search sortings two pointers *1500

Please click on ads to support us..

Python Code:

from typing import NamedTuple


class Friend(NamedTuple):
    money: int
    friendship_score: int


def split_in_groups(
    array: "list[Friend]",
    target: int,
):
    i = 0
    freezed_index = 0
    border = len(array) - 1
    max_score = 0
    score = 0

    while i <= border:
        difference = array[i].money - array[freezed_index].money

        while difference >= target:
            score -= array[freezed_index].friendship_score
            freezed_index += 1
            difference = array[i].money - array[freezed_index].money
        
        score += array[i].friendship_score
        
        if score > max_score:
            max_score = score
        i += 1
        

    return max_score


def collect_input():
    num_friends, target = [int(inputted) for inputted in input().split(" ")]
    friend_list = []
    for _ in range(num_friends):
        money, score = [int(inputted) for inputted in input().split(" ")]
        friend = Friend(money, score)
        friend_list.append(friend)
    friend_list.sort(key=lambda friend: friend.money)
    return target, friend_list


if __name__ == "__main__":
    target, friend_list = collect_input()
    max_score = split_in_groups(friend_list, target)
    print(max_score)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int,int>

#define pbb push_back
#define con continue
#define br break
#define len length
#define F first
#define S second
#define mem(dp) memset(dp,-1,sizeof(dp))
#define all(x) x.begin(),x.end()

#define sp system("pause");
#define deb cout << "\n*** HERE ***\n"
#define nl cout << "\n"

const int N = 10e5 + 10;
const int mod = 1e9 + 7;
#define fastIO ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

ll power(ll a, ll b) { a %= mod; ll res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; }
ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(a % b, b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }


/*       HERE CODE STARTS    */

void solve()
{
    int n, d; cin >> n >> d;
    vector<pi>arr;
    for (int i = 0; i < n; i++)
    {
        int a, b; cin >> a >> b;
        arr.pbb({ a,b });
    }
    sort(all(arr));
    int st = 0;
    ll fs = 0;
    ll res = 0;
    for(int i = 0;i<n;i++)
    {
        fs+=arr[i].S;
        while(st < i && abs(arr[st].F - arr[i].F)>=d)
        {
            fs-=arr[st].S;
            st++;
        }
        res = max(res,fs);
    }



    cout << res << endl;
}



int main()
{
    fastIO;
    int t = 1;
    while (t--) solve();



    return 0;
}


Comments

Submit
0 Comments
More Questions

1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness